import json
import numpy as np
import os

# 读取数据（将格子平铺成1维，输出25*45 =1125）


def read_json_to_matrix(base_path, rows, columns, times=24):

    # base_path = './bock_count_json'
    file_list = os.listdir(base_path)
    days = len(file_list)
    hours = times*days
    all_data = np.zeros((hours, rows*columns))
    # count_data = np.zeros((length, width))

    for j in range(days):
        path = base_path + '/' + file_list[j]
        with open(path) as f:
            load_dict = json.load(f)
            str0 = 'hour'
            for i in range(times):
                str_i = str(i)
                str_hour = str0 + str_i
                if str_hour in load_dict['data'].keys():
                    hour = load_dict['data'][str_hour]
                    hour = np.array(hour)
                    for m in range(rows):
                        for n in range(columns):
                            all_data[i+j*times][m*rows+n] = hour[m][n]
                            # count_data[m][n] = hour[m][n]+count_data[m][n]

    return all_data


def read_json_one_row(base_path, row, width, times=24):
    # base_path = './bock_count_json'
    file_list = os.listdir(base_path)
    days = len(file_list)
    all_data = np.zeros((1, width, times * days))
    count_data = np.zeros((1, width))

    for j in range(len(file_list)):

        path = base_path + '/' + file_list[j]
        with open(path) as f:
            load_dict = json.load(f)
            str0 = 'hour'
            for i in range(times):
                str_i = str(i)
                str_hour = str0 + str_i
                if str_hour in load_dict['data'].keys():
                    hour = load_dict['data'][str_hour]
                    hour = np.array(hour)
                    for n in range(width):
                        all_data[0][n][i + j * times] = hour[row][n]
                        count_data[0][n] = hour[row][n] + count_data[0][n]

    return all_data, count_data

